// Burner Shots Factory Test Dialog Rev. 0.02
// history:
// rev 0.00: hacked together code
// rev 0.01: Added sprite toggle.
// rev 0.02: now uses video redraw
//
// Known Problems:
// Swiching anything causes the frame to advance
//
// "ghosting" that appears on screen is not captured (this is a good thing)

#include "burner.h"

static HWND hSFactdlg = NULL;
unsigned char layerBackup;

static bool bOldPause;

void toggleLayer(unsigned char thisLayer)
{
	nBurnLayer ^= thisLayer; // xor with thisLayer
	videoInterface.redraw();
	videoInterface.paint(0);
}

void toggleSprite(unsigned char PriNum)
{
	nSpriteEnable ^= PriNum; // xor with thisLayer
	videoInterface.redraw();
	videoInterface.paint(0);
}

static int SFactdUpdate()
{

	CheckDlgButton(hSFactdlg, IDC_CHECK1, (nBurnLayer & 1) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_CHECK2, (nBurnLayer & 2) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_CHECK3, (nBurnLayer & 4) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_CHECK4, (nBurnLayer & 8) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE1, (nSpriteEnable & 0x01) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE2, (nSpriteEnable & 0x02) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE3, (nSpriteEnable & 0x04) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE4, (nSpriteEnable & 0x08) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE5, (nSpriteEnable & 0x10) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE6, (nSpriteEnable & 0x20) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE7, (nSpriteEnable & 0x40) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE8, (nSpriteEnable & 0x80) ? BST_CHECKED : BST_UNCHECKED);
	return 0;
}

static int SFactdInit()
{
	layerBackup = nBurnLayer;
	runPause = 1;
	CheckDlgButton(hSFactdlg, IDC_CHECK1, (nBurnLayer & 1) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_CHECK2, (nBurnLayer & 2) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_CHECK3, (nBurnLayer & 4) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_CHECK4, (nBurnLayer & 8) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE1, (nSpriteEnable & 0x01) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE2, (nSpriteEnable & 0x02) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE3, (nSpriteEnable & 0x04) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE4, (nSpriteEnable & 0x08) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE5, (nSpriteEnable & 0x10) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE6, (nSpriteEnable & 0x20) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE7, (nSpriteEnable & 0x40) ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hSFactdlg, IDC_SPRITE8, (nSpriteEnable & 0x80) ? BST_CHECKED : BST_UNCHECKED);
	return 0;
}

static int SFactdExit()
{
	nBurnLayer = layerBackup;
	nSpriteEnable = 0xFF;
	hSFactdlg = NULL;
	runPause = bOldPause;
	gameInpCheckMouse();
	return 0;
}

static INT_PTR CALLBACK SShotDialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	(void)lParam; (void)wParam; (void)hDlg;

	if (msg == WM_INITDIALOG) {
		hSFactdlg = hDlg;
		SFactdInit();
		return 0;
	}
	if (msg == WM_CLOSE) {
		DestroyWindow(hSFactdlg);
		dialogManager.remove(IDD_CAPTURE);
		return 0;
	}
	if (msg == WM_DESTROY) {
		SFactdExit();
		return 0;
	}

	if (msg == WM_COMMAND) {
		int Id = LOWORD(wParam);
		int Notify = HIWORD(wParam);

		if ((Id == IDOK || Id == IDCANCEL) && Notify == BN_CLICKED) {
			SendMessage(hDlg,WM_CLOSE,0,0);
			return 0;
		}
		else if (Id == IDC_CAPTURE && Notify == BN_CLICKED) {
			if (driverOkay) {
				makeScreenShot();
				SFactdUpdate();
			}
		}
		else if (Id == IDC_CAPTURE_PREV && Notify == BN_CLICKED) {
			if (driverOkay) {
				makeScreenShot(false);
				SFactdUpdate();
			}
		}
		else if (Id == IDC_CHECK1 && Notify == BN_CLICKED) {
			toggleLayer(1);
			SFactdUpdate();
		}
		else if (Id == IDC_CHECK2 && Notify == BN_CLICKED) {
			toggleLayer(2);
			SFactdUpdate();
		}
		else if (Id == IDC_CHECK3 && Notify == BN_CLICKED) {
			toggleLayer(4);
			SFactdUpdate();
		}
		else if (Id == IDC_CHECK4 && Notify == BN_CLICKED) {
			toggleLayer(8);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE1 && Notify == BN_CLICKED) {
			toggleSprite(0x01);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE2 && Notify == BN_CLICKED) {
			toggleSprite(0x02);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE3 && Notify == BN_CLICKED) {
			toggleSprite(0x04);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE4 && Notify == BN_CLICKED) {
			toggleSprite(0x08);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE5 && Notify == BN_CLICKED) {
			toggleSprite(0x10);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE6 && Notify == BN_CLICKED) {
			toggleSprite(0x20);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE7 && Notify == BN_CLICKED) {
			toggleSprite(0x40);
			SFactdUpdate();
		}
		else if (Id == IDC_SPRITE8 && Notify == BN_CLICKED) {
			toggleSprite(0x80);
			SFactdUpdate();
		}
		else if (Id == IDC_ADVANCE && Notify == BN_CLICKED) {
			videoInterface.frame();
			videoInterface.paint(0);
		}

		if (runPause) {
			audWriteSlience();
		}
	}
	return 0;
}

int shotFactoryCreate()
{
	if (hSFactdlg) {
		// already open so just reactivate the window
		SetActiveWindow(hSFactdlg);
		return 0;
	}

	bOldPause = runPause;
	runPause = 1;
	audioInterface.blank();

	hSFactdlg = localiseCreateDialog(IDD_CAPTURE, hScrnWnd, (DLGPROC)SShotDialogProc);
	if (!hSFactdlg) {
		return 1;
	}
	dialogManager.add(IDD_CAPTURE, hSFactdlg);

	wndInMid(hSFactdlg, hScrnWnd);
	ShowWindow(hSFactdlg, SW_NORMAL);
	SFactdUpdate();
	return 0;
}
